//
//  TestView.m
//  DMMCalibration
//
//  Created by apple on 17/3/1.
//  Copyright © 2017年 microtest.org. All rights reserved.
//

#import "TestView.h"
#import "Engine.h"

@interface TestView ()
{
    NSMutableArray *_tabledates;
    NSArray *_testItems;
    
    Engine *_engine;
    
   
    IBOutlet NSProgressIndicator *_process;
    IBOutlet NSTableView *tableView;
    NSInteger _stepRow;
}

@end

@implementation TestView

-(instancetype)init
{
    if([super init] == self)
    {
        
    }
    return self;
}

- (void)awakeFromNib
{
    _engine = [[Engine alloc] initWithObject:self];
    _tabledates = [[NSMutableArray alloc]init];
    _stepRow = 0;
}


// 为tableview添加源数据
-(void)tableViewInit:(NSDictionary *)dic
{
    [_tabledates addObject:dic];
    [tableView reloadData];
    [tableView scrollRowToVisible:_tabledates.count > 0 ? _tabledates.count-1 : 0];
    
}

- (void) tableViewInit:(NSDictionary *)dic row:(int)row
{
    [_tabledates removeObjectAtIndex:row];
    [tableView reloadData];
    [_tabledates insertObject:dic atIndex:row];
    
}

-(void)startTest
{
    [_engine excuteAll];
}

-(void)startExcute
{
    [_engine singleStepExcute:_stepRow];
}

-(void)setPowSupVolt
{
    [_engine setPowSupVoltage];
}

-(void)measBoardMeasVoltage
{
    [_engine measBoardMeasVolt];
}

-(void)dmmMeasVoltage
{
    [_engine dmmMeasVolt];
}

- (void)getReadVoltOffset
{
    [_engine getReadVoltOffset];
}

-(void)setReadVoltOffset
{
    [_engine setReadVoltOffset];
}

/**************   Table View implement   *****************/

- (NSUInteger) numberOfRowsInTableView:(NSTableView *)tableView
{
    return [_tabledates count];
}

- (id) tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    NSDictionary* dic = [_tabledates objectAtIndex:row];
    NSString* value = [dic objectForKey:[tableColumn identifier]];
    
    if (value != nil) {
        return value;
    }
    else {
        return @"error";
    }
}

- (void) tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    NSDictionary* value = [_tabledates objectAtIndex:row];
    [value setValue:object forKey:[tableColumn identifier]];
}


- (IBAction)tableViewClick:(NSTableView *)sender
{
    _stepRow = [sender clickedRow];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do view setup here.
}

@end